import http from './http'
import type { ApiResponse, PageResponse } from '@/types/api'

// 登录
export async function login(username: string, password: string) {
  const resp = await http.post<ApiResponse<{ token: string }>>('/auth/login', { username, password })
  return resp.data
}

export async function logout() {
  const resp = await http.post<ApiResponse<boolean>>('/auth/logout')
  return resp.data
}

export interface UserProfileVO {
  id: number
  username: string
  nickname?: string
  avatar?: string
  email?: string
  roles?: string
  perms?: string
}

export async function getProfile() {
  const resp = await http.get<ApiResponse<UserProfileVO>>('/auth/profile')
  return resp.data
}

export async function register(payload: { username: string; password: string; email?: string }) {
  const resp = await http.post<ApiResponse<boolean>>('/auth/register', payload)
  return resp.data
}

// 分类管理
export async function adminCreateCategory(payload: { name: string; description?: string; parentId?: number; sortOrder?: number; status?: number }) {
  const resp = await http.post<ApiResponse<boolean>>('/admin/categories', payload)
  return resp.data
}

export async function adminUpdateCategory(id: number, payload: { name: string; description?: string; parentId?: number; sortOrder?: number; status?: number }) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/categories/${id}`, payload)
  return resp.data
}

export async function adminDeleteCategory(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/categories/${id}`)
  return resp.data
}

// 标签管理
export async function adminCreateTag(payload: { name: string; color?: string }) {
  const resp = await http.post<ApiResponse<boolean>>('/admin/tags', payload)
  return resp.data
}

export async function adminUpdateTag(id: number, payload: { name: string; color?: string }) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/tags/${id}`, payload)
  return resp.data
}

export async function adminDeleteTag(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/tags/${id}`)
  return resp.data
}

// 评论管理
export interface CommentAdminVO {
  id: number
  articleId: number
  parentId: number
  content: string
  nickname?: string
  email?: string
  status: number
  createTime: string
}

export async function adminPageComments(params: { pageNo: number; pageSize: number; status?: number; articleId?: number }) {
  const resp = await http.get<ApiResponse<PageResponse<CommentAdminVO>>>('/admin/comments', { params })
  return resp.data
}

export async function adminApproveComment(id: number) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/comments/${id}/approve`)
  return resp.data
}

export async function adminRejectComment(id: number) {
  const resp = await http.put<ApiResponse<boolean>>(`/admin/comments/${id}/reject`)
  return resp.data
}

export async function adminDeleteComment(id: number) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/comments/${id}`)
  return resp.data
}

// 在线会话管理
export interface OnlineSessionVO {
  token: string
  userId: number
  username: string
  roles: string
  ttlSeconds: number
}

export async function listOnlineSessions(q?: string) {
  const params = q ? { q } : {}
  const resp = await http.get<ApiResponse<OnlineSessionVO[]>>('/admin/sessions', { params })
  return resp.data
}

export async function kickoutSession(token: string) {
  const resp = await http.delete<ApiResponse<boolean>>(`/admin/sessions/${token}`)
  return resp.data
}

// SEO 与站点地图管理
export async function generateSitemap() {
  const resp = await http.post<ApiResponse<boolean>>('/admin/sitemap/generate')
  return resp.data
}

export async function clearSitemapCache() {
  const resp = await http.delete<ApiResponse<boolean>>('/admin/sitemap/cache')
  return resp.data
}

export async function submitSitemap() {
  const resp = await http.post<ApiResponse<boolean>>('/admin/sitemap/submit')
  return resp.data
}


